const BaseRest = require('./rest.js');

module.exports = class extends BaseRest {

    async getAction() {

        let data;
        if (this.id) {
            const pk = this.modelInstance.pk;
            data = await this.modelInstance.where({ [pk]: this.id }).find();
            return this.success(data);
        }



        const method = this.get('method');
        if (method === 'getSchoolByCode') { //使用学校代码检索
            const school_code = this.get('school_code');
            data = await this.modelInstance.where({ school_code: school_code }).find();
            // think.logger.debug(data)
            return this.success(data);
        }

        let map = {};
        const key = this.get('key');
        if (key) {
            map['title|school_code'] = ['LIKE', '%' + key + '%']; //title LIKE '%KEY%' OR school_code LIKE '%KEY%'
        }

        const page = this.get('page'); //分页参数
        if (page) {
            const pageSize = this.get('pageSize') || 10; //分页参数
            data = await this.modelInstance.where(map).page(page, pageSize).countSelect();
            return this.success(data);
        } else {
            data = await this.modelInstance.where(map).select();      //不分页，检索全部记录
            return this.success(data);
        }


    }
};